/**
 * 
 */
package cl.darksigns.webstat.back.utils;

import org.apache.log4j.Logger;
import cl.darksigns.webstat.back.vo.WebStatVO;
import cl.darksigns.webstat.back.vo.HeaderVO;
import cl.darksigns.webstat.back.vo.SessionVO;
import cl.darksigns.webstat.back.vo.DataVO;
import cl.darksigns.webstat.back.dao.WebStatDao;
import cl.darksigns.webstat.back.exception.WebStatException;

import java.util.ArrayList;
import java.util.Iterator;

public class ProcessData {
    private static Logger logger = Logger.getLogger(ProcessData.class);
    private static ProcessData ourInstance = new ProcessData();

    /**
     *
     * @return
     */
    public static ProcessData getInstance() {
        return ourInstance;
    }

    private ProcessData() {
    }

    public synchronized void processInformation(WebStatVO data) throws WebStatException {
        WebStatDao dao = new WebStatDao();
        int reqId = dao.getNextId();
        logger.info("Inserting request id: " + reqId);
        dao.insertRequest(reqId,data.getRequestValues());
        logger.info("Inserting header data");
        ArrayList headerList = data.getHeaderValues();

        for(Iterator i = headerList.iterator(); i.hasNext();){
            HeaderVO header = (HeaderVO)i.next();
            dao.insertHeader(reqId,header);
        }
        logger.info("Inserting session data");
        ArrayList sessionList = data.getSessionValues();
        for(Iterator i = sessionList.iterator();i.hasNext();){
            SessionVO session = (SessionVO)i.next();
            dao.insertSession(reqId, session);
        }
        logger.info("Inserting data fields");
        ArrayList dataList = data.getDataValues();
        for(Iterator i = dataList.iterator();i.hasNext();){
            DataVO dataV = (DataVO)i.next();
            dao.insertData(reqId, dataV);
        }
        logger.info("Insert finished");
    }
    
    public synchronized void processSpecificInformation(WebStatVO data) throws WebStatException {
        
    }
}
